package com.lbmb.mdps.workbench.service.impl;

import com.lbmb.mdps.commons.domain.*;
import com.lbmb.mdps.commons.exception.InventoryException;
import com.lbmb.mdps.commons.exception.PurchaseException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.settings.dao.EmployeeDao;
import com.lbmb.mdps.settings.domain.Employee;
import com.lbmb.mdps.workbench.dao.*;
import com.lbmb.mdps.workbench.domain.*;
import com.lbmb.mdps.workbench.service.InventoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-06-01 2:16
 * description
 */
@Service("inventoryService")
public class InventoryServiceImpl implements InventoryService {

    @Resource
    private InventoryDao inventoryDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private WarehouseDao warehouseDao;
    @Resource
    private MedicineDao medicineDao;
    @Resource
    private MedicineTypeDao medicineTypeDao;
    @Resource
    private ThirdPartyDao thirdPartyDao;
    @Resource
    private DeliverDao deliverDao;
    @Resource
    private AuditDao auditDao;

    @Override
    public Integer getInventoryTotal(InventoryReceive inventoryReceive) throws InventoryException {
        /*
         * 获得库存总条数
         * @author 林文塨
         * @date 2022/6/1 8:22
         * @param [inventoryReceive]
         * @return java.lang.Integer
         */
        Integer total = inventoryDao.getInventoryTotal(inventoryReceive);

        if (total < 0) {
            throw new InventoryException("获取库存分页总条数失败！");
        }

        return total;
    }

    @Override
    public List<InventoryVo> inventoryPageList(InventoryReceive inventoryReceive) throws InventoryException {
        /*
         * 分页获取库存
         * @author 林文塨
         * @date 2022/6/1 8:22
         * @param [inventoryReceive]
         * @return java.util.List<com.lbmb.mdps.workbench.domain.Inventory>
         */
        List<InventoryVo> inventoryVoList = new ArrayList<>();

        List<Inventory> inventoryList = inventoryDao.inventoryPageList(inventoryReceive);

        if (inventoryList == null) {
            throw new InventoryException("分页获取库存列表列表失败！");
        }

        for (Inventory inventory : inventoryList) {

            InventoryVo inventoryVo = new InventoryVo();

            /*设置基本属性*/
            inventoryVo.setPurchaseNumber(inventory.getPurchaseNumber());
            inventoryVo.setDeliverNumber(inventory.getDeliverNumber());
            inventoryVo.setExistedNumber(inventory.getExistedNumber());

            /*设置供应商*/
            Supplier supplier = supplierDao.getSupplierById(inventory.getSupplier());
            inventoryVo.setSupplier(supplier.getName());
            inventoryVo.setSupplierId(supplier.getId());

            /*设置仓库*/
            Warehouse warehouse = warehouseDao.getWarehouseById(inventory.getWarehouse());
            inventoryVo.setWarehouse(warehouse.getName());
            inventoryVo.setWarehouseId(warehouse.getId());

            /*设置药品*/
            Medicine medicine = medicineDao.getMedicineById(inventory.getMedicine());

            /*设置药品类型*/
            MedicineType medicineType = medicineTypeDao.getMedicineTypeById(medicine.getMedicineType());
            medicine.setMedicineTypeVo(medicineType);

            inventoryVo.setMedicine(medicine);

            inventoryVoList.add(inventoryVo);
        }

        return inventoryVoList;
    }

    @Override
    @Transactional
    public void deliver(DeliverReceive deliverReceive) throws InventoryException {
        /*
         * 出库操作
         * @author 林文塨
         * @date 2022/6/1 21:36
         * @param [deliverReceive]
         * @return void
         */

        /*创建第三方*/
        ThirdParty thirdParty = new ThirdParty();
        thirdParty.setId(SnowFlakeIdUtil.generateId());
        thirdParty.setBuyer(deliverReceive.getBuyer());
        thirdParty.setContactNumber(deliverReceive.getContactNumber());
        thirdParty.setRemark(deliverReceive.getRemark());
        thirdParty.setAddress(deliverReceive.getAddress());
        thirdParty.setCreateTime(DateTimeUtil.getSysTime());
        thirdParty.setCreateBy(Long.valueOf(deliverReceive.getCurrentUserId()));
        thirdParty.setDetailAddress(deliverReceive.getDetailAddress());
        thirdParty.setDeleteFlag("0");

        int result1 = thirdPartyDao.addThirdPart(thirdParty);

        if (result1 != 1) {
            throw new InventoryException("创建第三方失败！");
        }

        /*创建出库订单*/
        Deliver deliver = new Deliver();
        deliver.setId(SnowFlakeIdUtil.generateId());
        deliver.setMedicine(deliverReceive.getMedicine());
        deliver.setAggregateAmount(deliverReceive.getAggregateAmount());
        deliver.setNumber(deliverReceive.getNumber());
        deliver.setOrderTime(DateTimeUtil.getSysTime());
        deliver.setOrderBy(Long.valueOf(deliverReceive.getCurrentUserId()));
        deliver.setDeleteFlag("0");
        deliver.setState("待审核");
        deliver.setThirdParty(thirdParty.getId());
        deliver.setSupplier(Long.valueOf(deliverReceive.getSupplierId()));
        deliver.setWarehouse(Long.valueOf(deliverReceive.getWarehouseId()));

        int result2 = deliverDao.addDeliver(deliver);

        if (result2 != 1) {
            throw new InventoryException("创建出库订单失败！");
        }

        /*创建审核表*/
        Audit audit = new Audit();
        audit.setId(SnowFlakeIdUtil.generateId());
        audit.setState("待审核");
        audit.setAuditOrder(deliver.getId());
        audit.setAuditBy(deliver.getOrderBy());
        audit.setAuditTime(DateTimeUtil.getSysTime());
        int result3 = auditDao.addAudit(audit);

        if (result3 != 1) {
            throw new InventoryException("创建审核表失败！");
        }

        /*更新库存*/
        Purchase purchase = new Purchase();
        purchase.setMedicine(deliverReceive.getMedicine());
        purchase.setSupplier(deliver.getSupplier());
        purchase.setWarehouse(deliver.getWarehouse());
        Inventory inventory = inventoryDao.getInventoryByCondition(purchase);

        inventory.setDeliverNumber(inventory.getDeliverNumber()+deliver.getNumber());
        inventory.setExistedNumber(inventory.getExistedNumber()-deliver.getNumber());
        int result4 = inventoryDao.updateInventory(inventory);

        if (result4 != 1) {
            throw new InventoryException("更新库存失败！");
        }
    }
}
